package com.hejing.线程安全问题;

/**
 * ClassName: WindowTest
 * Package: com.hejing.线程安全问题
 * Description:
 *      使用实现Runnable的方式，实现卖票
 * @Author: HeJing
 * @Create: 2023-04-14 19:21
 * @Version: 1.0
 */
public class WindowTest {
    public static void main(String[] args) {

        SaleTicket s1 = new SaleTicket();
        Thread t1 = new Thread(s1);
        Thread t2 = new Thread(s1);
        Thread t3 = new Thread(s1);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}

class SaleTicket implements Runnable {
    int ticket = 100;

    @Override
    public void run() {
        while(true) {
            if(ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "售票, 票号为: " + (101 - ticket));
                ticket--;
            } else {
                break;
            }
        }
    }
}
























